package com.itany.corejava.code13_jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author Miss Chen
 * @version 1.0
 * @date 2024年08月07日16:26
 */
public class Test07_多表保存 {

    public static void main(String[] args) throws DataAccessException {

        //使用外键字段关联
        // Dept dept=new Dept("research2","good");
        // Integer id=insertDeptReturnPrimary(dept);
        //
        // Emp emp1=new Emp("e1",6000.0,id);
        // Emp emp2=new Emp("e2",6400.0,id);
        // Emp emp3=new Emp("e3",6700.0,id);
        //
        // insertEmp(emp1);
        // insertEmp(emp2);
        // insertEmp(emp3);


        Dept dept=new Dept("caiwu","very good");
        Integer id=insertDeptReturnPrimary(dept);
        //使用对象属性关联
        dept.setId(id);
        Emp emp1=new Emp("e11",5000.0,dept);
        Emp emp2=new Emp("e22",23400.0,dept);
        Emp emp3=new Emp("e33",6600.0,dept);
        insertEmp(emp1);
        insertEmp(emp2);
        insertEmp(emp3);


    }

    public static Integer insertDeptReturnPrimary(Dept dept) throws DataAccessException {
        Connection conn=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        Integer id=null;
        try {
            conn=JDBCUtil.getConnection();
            String sql=new StringBuffer()
                    .append(" insert into ")
                    .append(" t_dept ")
                    .append(" (name,remark) ")
                    .append(" values ")
                    .append(" (?,?) ")
                    .toString();
            ps=conn.prepareStatement(sql,PreparedStatement.RETURN_GENERATED_KEYS);
            ps.setString(1,dept.getName());
            ps.setString(2,dept.getRemark());
            ps.executeUpdate();
            rs=ps.getGeneratedKeys();
            while(rs.next()){
                id=rs.getInt(1);
            }
            return id;

        } catch (DataAccessException e) {
            e.printStackTrace();
            throw e;
        } catch (SQLException e) {
            e.printStackTrace();
            throw new DataAccessException("数据库访问异常");
        } finally {
            JDBCUtil.close(conn,ps,rs);
        }
    }

    public static void insertEmp(Emp emp) throws DataAccessException {
        Connection conn=null;
        PreparedStatement ps=null;
        try {
            conn=JDBCUtil.getConnection();
            String sql=new StringBuffer()
                    .append(" insert into ")
                    .append(" t_emp ")
                    .append(" (name,salary,dept_id) ")
                    .append(" values ")
                    .append(" (?,?,?) ")
                    .toString();
            ps=conn.prepareStatement(sql);
            ps.setString(1,emp.getName());
            ps.setDouble(2,emp.getSalary());

            //使用外键字段关联
            // ps.setInt(3,emp.getDeptId());

            //使用对象关联
            ps.setInt(3,emp.getDept().getId());

            ps.executeUpdate();
        } catch (DataAccessException e) {
            e.printStackTrace();

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(conn,ps,null);
        }

    }


}
